﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HandBook.Data;
using Telerik.Web.UI;
using System.Data;

public partial class Admin_ProvinceManagement : BasePage
{
    public bool isDuplicate = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (ViewState["holdstate"] != null)
        {
            uxProvince.DataSource = ViewState["holdstate"];
        }
        RadComboBox1.Attributes.Add("onKeyPress", "javascript:if (event.keyCode == 13) __doPostBack('" + btnSearch.UniqueID + "','')");
    }
    protected void uxProvince_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        var proList = from p in HandBookData.EatDrinkDB.Provinces
                      select p;
        uxProvince.DataSource = proList;
    }
    protected void RadComboBox1_DataBound(object sender, EventArgs e)
    {
        RadComboBox1.Text = "Nhập vào từ khóa cần tìm";
    }
    protected void uxProvince_InsertCommand(object source, GridCommandEventArgs e)
    {
        GridEditableItem insertItem = e.Item as GridEditableItem;
        HandBook.Data.Province pr = new HandBook.Data.Province();
        pr.ProvinceName = (e.Item.FindControl("txtProvinceName") as TextBox).Text.Trim();

        //kiểm tra xem styleName đã có ở csdl hay chưa
        var proList = from p in HandBookData.EatDrinkDB.Provinces
                        select p;
        foreach (var item in proList)
        {
            if (item.ProvinceName.Equals(pr.ProvinceName))
            {
                isDuplicate = true;
                break;
            }
        }

        if (!isDuplicate)
        {
            HandBookData.EatDrinkDB.Provinces.InsertOnSubmit(pr);
            HandBookData.EatDrinkDB.SubmitChanges();
        }
        else
        {
            Response.Write("<script>return false;</script>");
        }
    }
    protected void uxProvince_UpdateCommand(object source, GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        int proID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["ProvinceID"]);

        var province = HandBookData.EatDrinkDB.Provinces.Single(p => p.ProvinceID == proID);
        string newProvinceName = (e.Item.FindControl("txtProvinceName") as TextBox).Text.Trim();

        //kiểm tra xem styleName đã có ở csdl hay chưa
        var proList = from s in HandBookData.EatDrinkDB.Provinces
                        where s.ProvinceID != proID
                        select s;
        foreach (var item in proList)
        {
            if (item.ProvinceName.Equals(newProvinceName))
            {
                isDuplicate = true;
                break;
            }
        }

        //thực hiện việc kiểm tra và update
        if (isDuplicate)
        {
            Response.Write("<script>return false;</script>");
        }
        else
        {
            province.ProvinceName = newProvinceName;
            HandBookData.EatDrinkDB.SubmitChanges();
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        EatDrinkDBDataContext context = new EatDrinkDBDataContext();

        string content = RadComboBox1.Text.Trim();
        string specialChar = "_+*:,.@{}[]()~$#%^&=|;?/<>!";
        int len = specialChar.Length;
        specialChar = specialChar.Trim();

        //lọc bỏ các ký tự đặc biệt
        for (int i = 0; i < len; i++)
        {
            if (content.Contains(specialChar[i]))
            {
                content = content.Replace(specialChar[i].ToString(), "");
            }
        }

        //tách chuỗi tìm kiếm
        string[] keywords = content.Split(' ');
        string where = string.Empty;
        foreach (string item in keywords)
        {
            if (where.Equals(string.Empty))
            {
                where = "ProvinceName LIKE '%" + item + "%'";
            }
            else
            {
                where += " OR ProvinceName LIKE '%" + item + "%'";
            }
        }
        string sql = "SELECT * FROM Province WHERE " + where;

        var proList = context.ExecuteQuery<Province>(sql);

        DataTable table = new DataTable();
        table.Columns.Add("ProvinceID");
        table.Columns.Add("ProvinceName");
        table.Columns.Add("Order");

        foreach (var item in proList)
        {
            DataRow row = table.NewRow();
            row["ProvinceID"] = item.ProvinceID;
            row["ProvinceName"] = item.ProvinceName;
            row["Order"] = item.Order;
            table.Rows.Add(row);
        }

        ViewState["holdstate"] = table;

        uxProvince.DataSource = table;
        uxProvince.Rebind();
    }
    protected void uxProvince_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == "ViewAll")
        {
            var proList = from r in HandBookData.EatDrinkDB.Provinces
                            select r;

            DataTable table = new DataTable();
            table.Columns.Add("ProvinceID");
            table.Columns.Add("ProvinceName");
            table.Columns.Add("Order");

            foreach (var item in proList)
            {
                DataRow row = table.NewRow();
                row["ProvinceID"] = item.ProvinceID;
                row["ProvinceName"] = item.ProvinceName;
                row["Order"] = item.Order;
                table.Rows.Add(row);
            }

            ViewState["holdstate"] = table;

            uxProvince.DataSource = table;
            uxProvince.Rebind();
        }
    }
}